/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package data;

import com.doxa.formTemplateSchema.FormControl;
import com.doxa.formTemplateSchema.FormTemplate;
import com.doxa.formTemplateSchema.FormTemplateProfile;
import com.doxa.formTemplateSchema.FormTemplateProfileDocument;
import com.doxa.formTemplateSchema.Property;
import control.bean.ControlBeanConstant;
import control.bean.ControlPropertiesBean;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.xmlbeans.XmlException;

/**
 *
 * @author Minsk
 */
public class FormTemplateParser {

    private File templateFile = new File("c:/formbuilder/formtemplate.xml");
    private FormTemplateProfileDocument doc;
    private FormTemplateProfile templateProfile;
    private FormTemplate template;
    private static final Logger log = LogManager.getLogger(FormTemplateParser.class);

    public FormTemplateParser() {
        reloadFile();
    }

    private void reloadFile() {
        if (templateFile.exists()) {
            System.out.println("FileExist");
            try {
                doc = FormTemplateProfileDocument.Factory.parse(templateFile);
                templateProfile = doc.getFormTemplateProfile();
                template = templateProfile.getFormTemplate();
            } catch (XmlException ex) {
                log.error("", ex);
                ex.printStackTrace();
            } catch (IOException ex) {
                log.error("", ex);
                ex.printStackTrace();
            }
        } else {
            doc = FormTemplateProfileDocument.Factory.newInstance();
            templateProfile = doc.addNewFormTemplateProfile();
            templateProfile.addNewFormMetaData();
            template = templateProfile.addNewFormTemplate();
        }

    }

    protected void setTemplateFile(File file) {
        templateFile = file;
        reloadFile();
    }
    
    protected ArrayList<ControlPropertiesBean> load() {
        ArrayList<ControlPropertiesBean> cpbTemplate = new ArrayList<ControlPropertiesBean>();
        
        FormControl[] formControlArray = template.getFormControlArray();
        for (FormControl fc : formControlArray) {
            ControlPropertiesBean cpb = new ControlPropertiesBean();
            HashMap<String, String> cpbProps = new HashMap<String, String>();
            
            cpbProps.put(ControlBeanConstant.CONTROLNAME, fc.getName());
            cpbProps.put(ControlBeanConstant.FIELDTYPE, fc.getControltype());
            cpbProps.put(ControlBeanConstant.WIDTH, fc.getWidth());
            cpbProps.put(ControlBeanConstant.HEIGHT, fc.getHeight());
            
            Property[] props = fc.getPropertyArray();
            for (Property p: props) {
                cpbProps.put(p.getId(), p.getPropValueArray(0));
            }
            cpb.setProps(cpbProps);
            cpbTemplate.add(cpb);
        }        

        return cpbTemplate;
    }

    protected boolean save(ArrayList<ControlPropertiesBean> cpbTemplate) {

        boolean saveSuccess = false;

        if (cpbTemplate == null || cpbTemplate.size() == 0) {
            return saveSuccess;
        }

        for (ControlPropertiesBean cpb : cpbTemplate) {
            HashMap<String, String> cpbProps = cpb.getProps();
            if (cpbProps == null || cpbProps.size() == 0) {
                continue;
            }

            if (!cpbProps.containsKey(ControlBeanConstant.CONTROLNAME)
                    | !cpbProps.containsKey(ControlBeanConstant.FIELDTYPE)
                    | !cpbProps.containsKey(ControlBeanConstant.WIDTH)
                    | !cpbProps.containsKey(ControlBeanConstant.HEIGHT)) {
                log.error("Invalid field! " + cpbProps);
                continue;
            }
            FormControl formControl = template.addNewFormControl();

            formControl.setId(cpbProps.get(ControlBeanConstant.CONTROLNAME));
            formControl.setName(cpbProps.get(ControlBeanConstant.CONTROLNAME));
            formControl.setWidth(cpbProps.get(ControlBeanConstant.WIDTH));
            formControl.setHeight(cpbProps.get(ControlBeanConstant.HEIGHT));
            formControl.setControltype(cpbProps.get(ControlBeanConstant.FIELDTYPE));
            
            cpbProps.remove(ControlBeanConstant.CONTROLNAME);
            cpbProps.remove(ControlBeanConstant.HEIGHT);
            cpbProps.remove(ControlBeanConstant.WIDTH);
            cpbProps.remove(ControlBeanConstant.FIELDTYPE);
            
            Iterator<String> iterator = cpbProps.keySet().iterator();
            while(iterator.hasNext()) {
                String key = iterator.next();
                String value = cpbProps.get(key);
                Property pp = formControl.addNewProperty();
                pp.setId(key);
                if (value == null) {
                    pp.addPropValue("");
                } else {
                    pp.addPropValue(value);
                }

            }            
        }
        save();
        saveSuccess = true;
        return saveSuccess;
    }

    private void save() {
        try {
            doc.save(templateFile);
        } catch (IOException ex) {
            log.error("", ex);
        }
    }
}
